package com.music.peacerhythms.Uakj;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;
import android.provider.Settings;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.music.peacerhythms.info.MusicInfo;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class UaMeth {

    public static void setAppScreenBrightness(int birghtessValue, Activity context) {
        Window window =context. getWindow();
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.screenBrightness = birghtessValue / 255.0f;
        window.setAttributes(lp);
    }

    public static int getScreenBrightness(Context context) {
        ContentResolver contentResolver = context.getContentResolver();
        int defVal = 125;
        return Settings.System.getInt(contentResolver,
                Settings.System.SCREEN_BRIGHTNESS, defVal);
    }

    public  static String format(long time){
        String day = time/(24*60*60*1000)>9?time/(24*60*60*1000)+"":"0"+time/(24*60*60*1000);
        long hourRemain = time%(24*60*60*1000);
        String hour = hourRemain/3600000>9?hourRemain/3600000+"":"0"+hourRemain/3600000;
        long minRemain = hourRemain%3600000;
        String min = minRemain/60000>9?minRemain/60000+"":"0"+minRemain/60000;
        long secRemain = minRemain%60000;
        String sec = secRemain/1000>9?secRemain/1000+"":"0"+secRemain/1000;
        return hour+":"+min+":"+sec+"";
    }


    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }


    public static ArrayList<ArrayList<MusicInfo>> getAllconfig(Context context) {
       SharedPreferences sharedPreferences = context.getSharedPreferences("configlist",0);
        String config = sharedPreferences.getString("config", "");
        ArrayList<ArrayList<MusicInfo>> musicInfos =new ArrayList<>();
            if (!config.equals("")){
                Type type = new TypeToken<ArrayList<List<MusicInfo>>>(){}.getType();
                musicInfos =   new Gson().fromJson(config,type);
            }

        return musicInfos;

    }

    public static void setconfiglist(Context context,String s) {
       SharedPreferences sharedPreferences = context.getSharedPreferences("configlist",0);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString("config",s);
        edit.commit();
    }

    public static void addConfig(Context context,ArrayList<MusicInfo> musicInfos) {
        ArrayList<ArrayList<MusicInfo>> allconfig = getAllconfig(context);
        allconfig.add(musicInfos);
        SharedPreferences sharedPreferences = context.getSharedPreferences("configlist",0);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString("config",new Gson().toJson(allconfig));
        edit.commit();
    }

    public static void removeConfig(Context context,int positon) {
        ArrayList<ArrayList<MusicInfo>> allconfig = getAllconfig(context);

        allconfig.remove(positon);

        SharedPreferences sharedPreferences = context.getSharedPreferences("configlist",0);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString("config",new Gson().toJson(allconfig));
        edit.commit();
    }

    public static void cgConfig(Context context,ArrayList<MusicInfo> musicInfos,int poi) {
        ArrayList<ArrayList<MusicInfo>> allconfig = getAllconfig(context);
        allconfig.set(poi,musicInfos);
        SharedPreferences sharedPreferences = context.getSharedPreferences("configlist",0);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        edit.putString("config",new Gson().toJson(allconfig));
        edit.commit();
    }


}
